package kr.co.doriclassic.pro14;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.FlowLayout;

import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IntPercent extends JFrame implements ActionListener {

	private IntPanelInput panel_input;
	private JPanel panel_button;
	private IntPanelLabel panel_label;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IntPercent frame = new IntPercent();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public IntPercent() {

		initialize();
	}

	private void initialize() {
		setTitle("이자 계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 185);
		getContentPane().setLayout(new BorderLayout(30, 10));
		
		panel_input = new IntPanelInput();
		GridLayout gridLayout = (GridLayout) panel_input.getLayout();
		gridLayout.setVgap(10);
		getContentPane().add(panel_input, BorderLayout.NORTH);
		
		panel_button = new JPanel();
		JButton btnResult = new JButton("변환");
		btnResult.setBackground(Color.BLUE);
		btnResult.setForeground(Color.WHITE);
		btnResult.addActionListener(this);
		panel_button.add(btnResult);
		panel_button.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		getContentPane().add(panel_button, BorderLayout.CENTER);
				
		panel_label = new IntPanelLabel();
		getContentPane().add(panel_label, BorderLayout.SOUTH);
		
		setResizable(false);
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		try {
			int account = Integer.parseInt(panel_input.getTfInput_account().trim());
			double rate = Double.parseDouble(panel_input.getTfInput_rate().trim());
			String interest = getInterest(account, rate)+"";
			panel_label.setTfResult("이자는 연"+interest+"만원입니다.");
		} catch (NumberFormatException e2) {
			JOptionPane.showMessageDialog(this, "숫자만 입력가능합니다.");
		}			
	}

	private double getInterest(int account, double rate) {
		return account * rate * 0.01;  		
	}
}
